import requests
import json

from html.parser import HTMLParser

def rpc( host, headers ):
    uri = host
    params = {}

    rspd = requests.get(uri,params=params,
                      headers=headers)

    rspd.encoding = rspd.apparent_encoding
                      
    return rspd.text

class BaseHTMLParser(HTMLParser):
    def __init__(self, host=None, headers=None, cell_type=None ):
        super(BaseHTMLParser, self).__init__()
        self._host = host
        self._headers = headers
        self._result = None
        # for parser
        self._cell_type = cell_type and cell_type or 'table'
        self._tag = ''
        self._tr = 0
        self._td = 0
        self._li = 0
        self._cell_no = (0, 0)
        self._cell = {}

        self._cell[self._cell_no] = {}
        # for parser


    
    def create_cell(self, tag=None):
        if self._cell_type == 'table':
            if tag == 'tr':
                self._tr += 1
                self._cell_no = (self._tr, 0)
                self._cell[self._cell_no] = {}
        
            elif tag == 'td':
                self._td += 1
                self._cell_no = (self._tr, self._td)
                self._cell[self._cell_no] = {}
        
        elif self._cell_type == 'li':
            if tag == 'li':
                self._li += 1
                self._cell_no = (self._li, 0)
                self._cell[self._cell_no] = {}
            
    def set_cell(self, tag, attr, value ):
        no = self._cell_no
        cell = self._cell.get( no, None )
        if cell == None:
            cell = {}
            self._cell[no] = cell
        
        tag_val = cell.get(tag, None)
        if tag_val == None:
            tag_val = {}
            cell[tag] = tag_val
        
        data = tag_val.get(attr, None)
        if data == None:
            data = []
            tag_val[attr] = data
        
        data += [value]
        

    def handle_starttag(self, tag, attrs):
        #print("Encountered a start tag:", tag)
        #print("Encountered a start tag:", tag, self.tr, self.td )
        self._tag = tag.lower()

        if self._tag in ("tr",'td','li'):   
            self.create_cell(self._tag)

        elif self._tag in [ "a", "td"]:   
            if len(attrs):
                for (variable, value) in attrs:   
                    var = variable.lower()
                    if var in ("href", "title"):
                        self.set_cell(self._tag, var, value)

        
    def handle_data(self, data):
        #print("Encountered some data  :", data)
        self.set_cell(self._tag, 'text', data)

        

    def _handle_result(self):
        results = {}
        
        for key, val in self._cell.items():
            x, y = key
            xx = results.get(x,None)
            if xx == None:
                xx = {}
                results[x] = xx
            
            xx[y] = val
        
        return results
        
        

    def rpc(self, url=None  ):
        url = url or self._host
        return rpc( url, self._headers )

    def parser(self, url=None, debug=None, *args, **kwargs ):
        html_code = self.rpc( url )
        self.feed(html_code)
        self.close()
        self._result = self._handle_result()

        return self.handle_result(debug=debug)

    @property
    def result(self):
        return self._result
        
    def handle_result_attr(self, vals, meta):

        if( len(vals) <= meta[0] ):
            return None, None
        
        attr = vals[meta[0]]
        
        for mm in meta[1:]:
            attr = attr.get(mm,{})
            
        if not attr:
            return None, None
            
        return 1, attr[0]

    def handle_result_date(self, date):
        return date

    def handle_result_href(self, href):
        return href

    def handle_result_title(self, title):
        return title



    def handle_result_0(self,debug, vals):
        result= {}
        
        for key,value in self.result_meta.items():
            if key == 'len':
                continue
            
            
            ret, data = self.handle_result_attr(vals, value)

            if key == 'href' and ret:
                data = self.handle_result_href( data )

            if key == 'date' and ret:
                data = self.handle_result_date( data )

            if key == 'title' and ret:
                data = self.handle_result_title( data )

            result[key] = data
            
        return result
            

    def handle_result(self,debug):
        results = []
        
        for tr, link in self.result.items():
            if len(link) < self.result_meta['len']:
                if not debug:
                    continue

            vals = list( link.values() )
            
            result = self.handle_result_0( debug, vals)
            
            if not result:
                if not debug:
                    continue
            
            results += [result]

        def fn(index, res):
            res['id'] = index+1
            return res


        results = [ fn(index, res) for index, res in enumerate( results ) ]
        return results

